﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GosWeb.Models.Db;

namespace GosWeb.Controllers
{

    public class GRatioController : Controller
    {

        GosDBContext db = GosDBContext.GetInstance();

        public ActionResult Index()
        {
            var genes = from m in db.GeneRatios select m;

            return View(genes.ToList());
        }

        public ActionResult Create(string id)
        {
            GeneRatio gr = new GeneRatio() { AnID = id };
            return View(gr);
        }

        [HttpPost]
        public ActionResult Create(GeneRatio newRate)
        {      
            newRate.GeneCode = newRate.GeneCode.Trim().ToUpper();
            if (ModelState.IsValid)
            {
                db.GeneRatios.Add(newRate);
                db.SaveChanges();
                return RedirectToAction("Details", "Biont", new { id = newRate.AnID });
            }
            else
            {
                return View(newRate);
            }
        }

        public ActionResult Details(Int64 id)
        {
            GeneRatio gr = db.GeneRatios.Find(id);
            if (gr == null)
            {
                string err = "未找到！";
                return RedirectToAction("Show", "Error", new { ErrorString = err });
            }
            else
            {
                //Location originalValues = db.Entry(biont).ComplexProperty(u => u.Address).OriginalValue;    
                return View("Details", gr);
            }
        }

        public ActionResult Edit(Int64 id)
        {
            GeneRatio gr = db.GeneRatios.Find(id);
            if (gr == null)
            {
                string err = "未找到！";
                return RedirectToAction("Show", "Error", new { ErrorString = err });
            }
            return View(gr);
        }


        [HttpPost]
        public ActionResult Edit(GeneRatio model)
        {
            try
            {
                var gr = db.GeneRatios.Find(model.GrID);
                model.GeneCode = model.GeneCode.Trim().ToUpper();
                UpdateModel(gr);
                db.SaveChanges();
                return RedirectToAction("Details", "Biont", new { id = gr.AnID });
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Edit Failure, see inner exception");
            }

            return View(model);
        }

        public ActionResult Delete(Int64 id)
        {
            var gr = db.GeneRatios.Find(id);
            if (gr == null)
            {
                string err = "未找到！";
                return RedirectToAction("Show", "Error", new { ErrorString = err });
            }
            return View(gr);
        }

        [HttpPost]
        public RedirectToRouteResult Delete(Int64 id, FormCollection collection)
        {
            GeneRatio gr = db.GeneRatios.Find(id);
            if (gr != null)
            {
                string anid = gr.AnID;
                db.GeneRatios.Remove(gr);
                db.SaveChanges();

                return RedirectToAction("Details", "Biont", new { id = anid });
            }
            else
            {
                return null;
            }
        }

    }
}
